import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/23 15:26
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }

    /**
     * 利用treeMap 按照 key排序，由于相同长度的字符添加到list时已顺序添加，可保证有序
     * @param text text
     * @return string
     */
    public String arrangeWords(String text) {
        text = text.toLowerCase();
        String[] t = text.split(" ");
        TreeMap<Integer, List<String >> treeMap = new TreeMap<>();
        for (String temp : t) {
            int n = temp.length();
            if (!treeMap.containsKey(n)) {
                treeMap.put(n, new ArrayList<>());
            }
            treeMap.get(n).add(temp);
        }
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<Integer, List<String>> entry: treeMap.entrySet()) {
            for (String a: entry.getValue()) {
                sb.append(a).append(" ");
            }
        }
        // 删除最后一个空格
        sb.deleteCharAt(sb.length() - 1);
        char[] c = sb.toString().toCharArray();
        c[0] -= 32;
        return new String(c);
    }
}
